package appConfig

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/appconfig"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertAppConfigLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增或修改下载配置
func NewInsertAppConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertAppConfigLogic {
	return &InsertAppConfigLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InsertAppConfigLogic) InsertAppConfig(req *types.InsertAppConfigReq) (resp *types.Empty, err error) {
	u, err := l.svcCtx.SettingService.AppConfig.UpdateAppConfig(l.ctx, &appconfig.UpdateAppConfigReq{
		MerchantId:            1,
		RightOpen:             rpc.StatusType(req.RightOpen),     // 首页右上角APP下载开关,0:关闭,1:开启
		IconOpen:              rpc.StatusType(req.IconOpen),      // 首页APP icon下载开关,0:关闭,1:开启
		IosDownUrl:            req.IosDownUrl,                    // 苹果下载地址
		IosVersion:            req.IosVersion,                    // ios版本
		IosHousingVersion:     req.IosHousingVersion,             // ios套壳版本号
		AndroidDownUrl:        req.AndroidDownUrl,                // 安卓下载地址
		AndroidVersion:        req.AndroidVersion,                // 安卓版本号
		AndroidHousingVersion: req.AndroidHousingVersion,         // 安卓套壳版本号
		IsForceUpdate:         rpc.StatusType(req.IsForceUpdate), // 是否强制更新,0:否,1:是
	})

	if err != nil {
		logc.Error(l.ctx, "InsertAppConfig err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}
	resp = &types.Empty{}
	return
}
